<template>
    <div>
        <el-table
            :data="orderList"
            style="width: 100%"
            row-class-name="订单管理系统">
            <el-table-column
              prop="createdAt"
              label="日期"
              width="180">
            </el-table-column>
            <el-table-column
              prop="receiver"
              label="姓名"
              width="180">
            </el-table-column>
            <el-table-column
              prop="address"
              width="280"
              label="地址">
            </el-table-column>
            <el-table-column
              prop="isPayed"
              label="支付状态">
            </el-table-column>
            <el-table-column
                fixed="right"
                label="操作"
                width="280">
                <template slot="header" slot-scope="scope">
                  <el-input
                    size="mini"
                    class="inline-input"
                    v-model="search"
                    :fetch-suggestions="querySearch"
                    placeholder="请输入关键字搜索"
                    @select="handleSelect"
                    @blur="changeList"/>
                </template>
                <template slot-scope="scope">
                    <el-button @click="handleClick(scope.row)" type="text" size="small">查看</el-button>
                    <el-button type="text" style="color:red" size="small" @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>


        <el-dialog size="mini" title="订单详情" :visible.sync="orderVisible">
          <el-form size="mini" :model="order">
            <el-form-item label="收货人" :label-width="formLabelWidth">
              <el-input :disabled="true" v-model="order.receiver" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="订单号" :label-width="formLabelWidth">
              <el-input v-model="order.no" :disabled="true" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="订单价格" :label-width="formLabelWidth">
              <el-input :disabled="true" v-model="order.price" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="收获地址" :label-width="formLabelWidth">
              <el-input :disabled="true" v-model="order.address" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="支付状态" :label-width="formLabelWidth">
                <el-switch
                :width="50"
                v-model="value"
                @change="onchange"
                active-color="#13ce66"
                inactive-color="#ff4949">
                </el-switch>
            </el-form-item>
            
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="orderVisible = false">确 定</el-button>
          </div>
        </el-dialog>
    </div>
</template>

<script>
import * as api from '../api/order'
export default {
    name:'order',
    data(){
        return{
            orderList:[],
            orderVisible:false,
            order:{},
            value:false,
            formLabelWidth:'200px',
            id:'',
            search:'',
            orders:[],
            title:'订单管理'
        }
    },
    watch:{
        value:function(val,oldValue){
            console.log(val)
        },
    },
    methods:{
        //查询
       querySearch(queryString, cb) {
            var restaurants = this.orders;
            var results = queryString ? orders.filter(this.createFilter(queryString)) : orders;
            // 调用 callback 返回建议列表的数据
            cb(results);
        },
        createFilter(queryString) {
            return (restaurant) => {
                return (restaurant.value.toLowerCase().indexOf        (queryString.toLowerCase()) === 0);
            };
        },
        handleSelect(item){
            console.log(item)
            this.changeList()
        },
        changeList(){
            console.log(this.search)
            api.getOrders({per:1000}).then(data=>{
                let arr = data.data.orders
                let newArr = []
                arr.map(v=>{
                    v.createdAt = v.createdAt.slice(0,10)
                    if(v.isPayed==false){
                        v.isPayed = '未支付'
                    }else{
                        v.isPayed = '已支付'
                    }
                    if(v.no.indexOf(this.search)!=-1){
                        newArr.push(v)
                    }
                })
                this.orderList = newArr
            })
            
        },


        payed(val,index){
            let orderId = val._id
            api.updateOrder({id:orderId,isPayed:true}).then(data=>{
                console.log(data)
            })
        },
        //查看信息
        handleClick(val){
            console.log(val._id)
            this.id = val._id
            this.orderVisible = true
            api.detailOrder(val._id).then(data=>{
                console.log(data)
                this.order = data.data
                this.value = data.data.isPayed
            })
        },

        //删除订单
        handleDelete(val){
            console.log(val._id)
            this.$confirm('请确认是否删除', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'danger'
            }).then(() => {
                this.$message({
                  type: 'success',
                  message: '删除成功!'
                });
                api.deleteOrder(val._id).then(data=>{
                    console.log(data)
                    let arr = this.orderList
                    arr.map(function(v,i){
                        if(v._id==val._id){
                            console.log(i)
                            arr.splice(i,1)
                        }
                    })
                    this.orderList = arr
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });          
                this.value = false
            });
        },



        //是否支付按钮
        onchange(v){
            this.$confirm('请确认是否修改', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'info'
            }).then(() => {
                this.$message({
                  type: 'success',
                  message: '修改成功!'
                });
                api.updateOrder({id:this.id,isPayed:v}).then(data=>{
                    api.getOrders().then(data=>{
                        console.log(data)
                        this.orderList = data.data.orders
                        this.orderList.map(v=>{
                            v.createdAt = v.createdAt.slice(0,10)
                            if(v.isPayed==false){
                                v.isPayed = '未支付'
                            }else{
                                v.isPayed = '已支付'
                            }
                        })
                        console.log(this.orderList)
                    })
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消修改'
                });          
            });
           
        }
    },
    mounted(){
        this.$emit('toparent',this.title)
        api.getOrders({per:1000}).then(data=>{
            console.log(data)
            this.orderList = data.data.orders
            let arr = []
            this.orderList.map(v=>{
                v.createdAt = v.createdAt.slice(0,10)
                if(v.isPayed==false){
                    v.isPayed = '未支付'
                }else{
                    v.isPayed = '已支付'
                }
                console.log(v.no)
                arr.push(v.no)
                arr.push(v.receiver)
            })
            this.orders = arr
        })
    }
}
</script>

<style scoped>
    h2{
        color:#fff;
    }
    .el-table .warning-row {
        background: oldlace;
    }

    .el-table .success-row {
        background: #f0f9eb;
    }
</style>


